<template>
    <view>
        <view class="topbar" :style="'--color:' + color + ';--rgbcolor:' + rgbcolor">
            <text @tap="topbarFn" :class="'item ' + (currentTab == index ? 'active' : '')" :data-idx="index" v-for="(item, index) in topbar" :key="index">{{ item }}</text>
        </view>
        <view class="nodata" v-if="LIST.length == 0">
            <image mode="aspectFit" src="/static/res/img/nodata.png"></image>
            暂无礼物
        </view>
        <view :class="currentTab == 0 ? 'my_gift' : 'giflist'" :style="'--color:' + color + ';--rgbcolor:' + rgbcolor" v-else>
            <block v-if="currentTab == 0">
                <checkbox-group @change="checkboxChange">
                    <view class="dl" v-for="(item, list_index) in LIST" :key="list_index">
                        <view @tap="uFn" class="dt" :data-uid="item.uid">
                            <image mode="aspectFill" :src="item.picurl"></image>
                        </view>

                        <view class="dd">
                            <view class="h4">
                                {{ item.title }}
                                <text class="i ico">{{ item.price }}</text>
                            </view>
                            <view @tap.stop.prevent="uFn" class="h6" :data-uid="item.uid">
                                <text class="time">{{ item.addtime }}</text>
                                <text>来自：</text>
                                <image class="ugrade1" mode="aspectFill" :src="item.grade_ico"></image>
                                <text>{{ item.nickname }}</text>
                            </view>
                        </view>

                        <view class="chkbox">
                            <label>
                                <checkbox :checked="item.checked" :value="item.id"></checkbox>
                                <view>兑换</view>
                            </label>
                        </view>
                    </view>
                </checkbox-group>
                <view class="btnbox">
                    <li>
                        <label @tap.stop.prevent="allFn">
                            <checkbox :checked="alled"></checkbox>
                            <text>全选</text>
                        </label>
                    </li>
                    <li @tap.native.stop.prevent="submitFn" :class="onoff">
                        <i class="ico"></i>
                        兑换
                    </li>
                </view>
            </block>
            <block v-else>
                <view class="li" v-for="(itemli, index) in LIST" :key="index">
                    <view>
                        <image class="picurl" mode="aspectFill" :src="itemli.picurl"></image>
                        <view class="h4">{{ itemli.title }}</view>
                        <view class="h5">
                            <text class="i ico"></text>
                            {{ itemli.price }}
                        </view>
                        <view class="h6">{{ itemli.addtime }}</view>
                        <view @tap.stop.prevent="uFn" class="h6" :data-uid="itemli.uid">
                            <text>{{ currentTab == 1 ? '送给' : '来自' }}</text>
                            <image class="ugrade1" mode="aspectFill" :src="itemli.grade_ico"></image>
                            <text>{{ itemli.nickname }}</text>
                        </view>
                    </view>
                </view>
                <view class="blank">　　　</view>
            </block>
        </view>
        <view @tap="backtopFn" class="backtop fadeInUp" data-id="index" :style="'color:' + color" v-if="backtop_flag">
            <text class="ico icon-backtop"></text>
            顶部
        </view>
    </view>
</template>

<script>
var t = require('../../@babel/runtime/helpers/defineProperty');
var a = require('../../res/www.a9a.cn.js');
var app = getApp();
var o = uni.getStorageSync('COOK_U');
export default {
    data() {
        return {
            topbar: ['我收的', '我送的', '我兑的'],
            checkarr: [],
            alled: false,
            onoff: 'off',
            currentTab: 0,
            LIST_index: 0,
            backtop_flag: false,
            color: '',
            rgbcolor: '',
            LIST: '',
            mask_class: '',
            list_index: []
        };
    },
    onLoad: function (t) {
        this.onLoadClone3389(t);
    },
    onPageScroll: function (t) {
        t.scrollTop > 100
            ? this.setData({
                  backtop_flag: true
              })
            : this.setData({
                  backtop_flag: false
              });
    },
    methods: {
        onLoadClone3389: function (t) {
            var o = t.t;
            o = a.empty(o) ? 1 : o;
            this.setData({
                currentTab: o - 1
            });
            var that = this;
            app.globalData.chklogin().then(function (t) {
                t || uni.navigateBack();
            });
            app.globalData.get_openid().then(function (t) {
                that.setData({
                    color: app.globalData._ZEAI.color,
                    rgbcolor: a.c16_rgb(app.globalData._ZEAI.color)
                });
                that.get_data(o);
            });
        },

        topbarFn: function (t) {
            var a = parseInt(t.currentTarget.dataset.idx);
            this.setData({
                currentTab: a
            });
            this.get_data(a + 1);
        },

        get_data: function (t) {
            var that = this;
            o = uni.getStorageSync('COOK_U');
            app.globalData.ajax({
                url: 'my_gift.php',
                data: {
                    submitok: 'list',
                    t: t,
                    openid: o.openid
                },
                success: function (t) {
                    1 == t.flag
                        ? that.setData({
                              LIST: t.LIST
                          })
                        : uni.showModal({
                              content: t.msg,
                              confirmColor: that.color,
                              confirmText: '朕知道了',
                              showCancel: false,
                              success: function (t) {
                                  uni.navigateBack();
                              }
                          });
                }
            });
        },

        uFn: function (t) {
            var a = t.currentTarget.dataset.uid;
            uni.navigateTo({
                url: '/pagesA/u/u?uid=' + a
            });
        },

        checkboxChange: function (t) {
            var a = t.detail.value;
            a.length > 0
                ? this.setData({
                      onoff: 'on',
                      checkarr: a
                  })
                : this.setData({
                      onoff: 'off',
                      checkarr: a,
                      alled: false
                  });
        },

        allFn: function () {
            var t;
            var a;
            var e = this.LIST;
            var o = e.length;
            var n = this.alled;
            var c = [];
            if (n) {
                for (t = 0; t < o; t++) {
                    e[t].checked = false;
                    c = [];
                }
                n = false;
                a = 'off';
            } else {
                for (t = 0; t < o; t++) {
                    e[t].checked = true;
                    c.push(e[t].id);
                }
                n = true;
                a = 'on';
            }
            this.setData({
                onoff: a,
                alled: n,
                LIST: e,
                checkarr: c
            });
        },

        submitFn: function (t) {
            var that = this;
            var c = that.checkarr;
            if (c.length < 1) {
                uni.showToast({
                    title: '请选择要兑换的礼物',
                    icon: 'none',
                    mask: true
                });
            } else {
                uni.showLoading({
                    title: '兑换中',
                    mask: true
                });
                var i = c.join(',');
                app.globalData.ajax({
                    url: 'my_gift.php',
                    data: {
                        submitok: 'ajax_duihuan',
                        openid: o.openid,
                        idlist: i
                    },
                    success: function (t) {
                        uni.hideLoading();
                        if (1 == t.flag) {
                            uni.showModal({
                                content: t.msg,
                                confirmColor: that.color,
                                confirmText: '朕知道了',
                                showCancel: false,
                                success: function (t) {
                                    that.onLoadClone3389(1, {});
                                }
                            });
                        } else {
                            t.msg;
                            t.this = that;
                            a.sorry(t);
                        }
                    }
                });
            }
        },

        mask_closeFn: function (a) {
            var that = this;
            var o = a.currentTarget.dataset.maskid;
            this.setData({
                mask_class: 'alpha100_0'
            });
            setTimeout(function () {
                that.setData(t({}, o, false));
            }, 500);
        },

        backtopFn: function (t) {
            uni.pageScrollTo({
                scrollTop: 0,
                duration: 300
            });
        }
    }
};
</script>
<style>
@import 'gift.css';
</style>
